package com.gitee.feizns.itrip.personal.auth.controller;

import com.gitee.feizns.itrip.personal.auth.entity.ao.ForAcceptActivate;
import com.gitee.feizns.itrip.personal.auth.entity.ao.ForAcceptCkusr;
import com.gitee.feizns.itrip.personal.auth.entity.ao.ForAcceptDoRegister;
import com.gitee.feizns.itrip.personal.auth.service.RegisterService;
import com.gitee.feizns.itrip.personal.beans.dto.Dto;
import com.gitee.feizns.itrip.personal.beans.pojo.User;
import com.gitee.feizns.itrip.personal.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author feizns
 * @since 2018/12/26 0026
 */
@RestController
@RequestMapping("/api")
public class RegisterController {

    @Autowired
    private RegisterService registerService;

    @GetMapping("/ckusr")
    public Dto ckusr(@Validated ForAcceptCkusr name) {
        return Dto.getSuccessDto(registerService.availableUserCode(name.getName()) ? "用户名可用" : "用户名不可用");
    }

    @PostMapping("/doregister")
    public Dto doregister(@RequestBody @Validated ForAcceptDoRegister user) {
        User newUser = Utils.copyTo(user, User.class);;
        registerService.emailRegister(newUser);
        return Dto.getSuccessDto("注册成功请尽快激活.");
    }

    @PutMapping("/activate")
    public Dto activate(@Validated ForAcceptActivate forAcceptActivate) {
        registerService.emailActivated(forAcceptActivate.getUser(), forAcceptActivate.getCode());
        return Dto.getSuccessDto("激活成功");
    }

    @PutMapping("/validatephone")
    public Dto validatephone(@Validated ForAcceptActivate forAcceptActivate) {
        registerService.phoneActivated(forAcceptActivate.getUser(), forAcceptActivate.getCode());
        return Dto.getSuccessDto("激活成功");
    }

    @PostMapping("/registerbyphone")
    public Dto registerbyphone(@RequestBody @Validated ForAcceptDoRegister user) {
        User newUser = Utils.copyTo(user, User.class);
        registerService.phoneRegister(newUser);
        return Dto.getSuccessDto("注册成功请尽快激活.");
    }

}